package com.ns.core.utils.bean;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 手动获取服务
 *
 * @author lw
 * @since 2024-11-12
 */
@Component
public class ServiceUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 根据spring托管对象的beanName获取实力对象;
     *
     * @param clazz
     * @return
     * @author khy
     * @createTime 2020年9月3日下午1:47:03
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
